package com.lfw.msmservice.service.impl;

import com.lfw.msmservice.service.EmailService;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

@Service
public class EmailServiceImpl implements EmailService {
    //发送邮箱验证码
    @Override
    public boolean send(Map<String, Object> param, String email, String code) {
        if (StringUtils.isEmpty(email)) return false;
        try {
            HtmlEmail htmlEmail = new HtmlEmail();//创建一个HtmlEmail实例对象
            htmlEmail.setHostName("smtp.qq.com");//邮箱的SMTP服务器，一般123邮箱的是smtp.123.com,qq邮箱为smtp.qq.com
            htmlEmail.setCharset("utf-8");//设置发送的字符类型
            htmlEmail.addTo(email);//设置收件人
            htmlEmail.setFrom("2034673742@qq.com","LFW");//发送人的邮箱为自己的，用户名可以随便填
            htmlEmail.setAuthentication("2034673742@qq.com","ybeepkgwdqbdcfec");//设置发送人到的邮箱和用户名和授权码(授权码是自己设置的)
            htmlEmail.setSubject("谷粒学院");//设置发送主题
            htmlEmail.setMsg("欢迎注册谷粒学院，您的验证码是：" + code + " 验证码在5分钟内有效！");//设置发送内容
            htmlEmail.send();//进行发送
        } catch (EmailException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
